package com.qianying.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.entity.OrderType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 订单类型Mapper接口
 */
@Mapper
public interface OrderTypeMapper extends BaseMapper<OrderType> {

    /**
     * 分页查询订单类型（使用@Select注解）
     */
    @Select("SELECT * FROM order_type " +
            "WHERE 1=1 " +
            "<if test='orderTypeName != null and orderTypeName != \"\"'>" +
            "AND order_type_name LIKE CONCAT('%', #{orderTypeName}, '%') " +
            "</if>" +
            "ORDER BY id ASC")
    IPage<OrderType> selectPage(
            Page<OrderType> page,
            @Param("orderTypeName") String orderTypeName);

    /**
     * 按名称查询订单类型（用于唯一性校验）
     */
    @Select("SELECT COUNT(*) FROM order_type WHERE order_type_name = #{orderTypeName}")
    int countByTypeName(@Param("orderTypeName") String orderTypeName);

    /**
     * 按代号查询订单类型（用于唯一性校验）
     */
    @Select("SELECT COUNT(*) FROM order_type WHERE order_type_code = #{orderTypeCode}")
    int countByTypeCode(@Param("orderTypeCode") String orderTypeCode);

    /**
     * 更新时按名称查询（排除当前ID）
     */
    @Select("SELECT COUNT(*) FROM order_type WHERE order_type_name = #{orderTypeName} AND id != #{id}")
    int countByTypeNameExcludeId(@Param("orderTypeName") String orderTypeName, @Param("id") Long id);

    /**
     * 更新时按代号查询（排除当前ID）
     */
    @Select("SELECT COUNT(*) FROM order_type WHERE order_type_code = #{orderTypeCode} AND id != #{id}")
    int countByTypeCodeExcludeId(@Param("orderTypeCode") String orderTypeCode, @Param("id") Long id);
}
